* Define shortcut to request file on PC which has cusips and edates. The edates 
are the fiscal year dates of firms listed on execucomp;
*To use this program you must have eventus and access to WRDS;
filename pcreqst 'C:\wald\return_input.txt';
/* Sign on to WRDS wald.inputcar
Note that the next line must be found in all eventus programs
as opposed to the line following that*/
%let wrds = localhost 4016;
options comamid=TCP remote=WRDS;
options nocenter linesize=92;
signon username=_prompt_;
rsubmit;

/* Define shortcut to request file on WRDS Unix server. */
/* ~/ means use my home directory. */
/* File name need not be same on both systems. */
/* Make WRDS shortcut be request as expected by Eventus. */
filename request '~/demorequest.txt';
proc upload infile=pcreqst outfile=request; run;


Eventus getdata monthly;
dateconv autodate cusiperm AllowMissingDate outsas=work.request ndays=36 ;



/* Run the event study and create an output SAS data set. */

/* shift1 tells the eventus to go one month forward from the fiscal year date
provided in C:\wald\return_input.txt;  The next data step gets the returns*/


Eventus getdata monthly;
request insas=work.request_cln  datefmt=crsp shift1 = 1 ndays=36;
Returns hsas outsas=work.returns ;

data ret;
set returns; 

rawret12 =  (1+retn1)*(1+retn2)*(1+retn3)*(1+retn4)*(1+retn5)* (1+retn6)*(1+retn7)*(1+retn8)*(1+retn9)*(1+retn10)*(1+retn11)*(1+retn12);
rawret24=  (1+retn1)*(1+retn2)*(1+retn3)*(1+retn4)*(1+retn5)* (1+retn6)*(1+retn7)*(1+retn8)*(1+retn9)*(1+retn10)*(1+retn11)*(1+retn12)*
(1+retn13)*(1+retn14)*(1+retn15)*(1+retn16)*(1+retn17)* (1+retn18)*(1+retn19)*(1+retn20)*(1+retn21)*(1+retn22)*(1+retn23)*(1+retn24);
rawret36 =(1+retn1)*(1+retn2)*(1+retn3)*(1+retn4)*(1+retn5)* (1+retn6)*(1+retn7)*(1+retn8)*(1+retn9)*(1+retn10)*(1+retn11)*(1+retn12)*
(1+retn13)*(1+retn14)*(1+retn15)*(1+retn16)*(1+retn17)* (1+retn18)*(1+retn19)*(1+retn20)*(1+retn21)*(1+retn22)*(1+retn23)*(1+retn24)*
(1+retn25)*(1+retn26)*(1+retn27)*(1+retn28)*(1+retn29)* (1+retn30)*(1+retn31)*(1+retn32)*(1+retn33)*(1+retn34)*(1+retn35)*(1+retn36);


mret12 =   (retn1   +retn2   +retn3   +retn4   +retn5    +retn6   +retn7   +retn8   +retn9   +retn10   +retn11   +retn12)/12 ;
mret24=   (retn1  +retn2  +retn3  +retn4  +retn5   +retn6  +retn7  +retn8  +retn9  +retn10  +retn11  +retn12 
 +retn13  +retn14  +retn15  +retn16  +retn17   +retn18  +retn19  +retn20  +retn21  +retn22  +retn23  +retn24)/24;
mret36 = (retn1  +retn2  +retn3  +retn4  +retn5   +retn6  +retn7  +retn8  +retn9  +retn10  +retn11  +retn12 
 +retn13  +retn14  +retn15  +retn16  +retn17   +retn18  +retn19  +retn20  +retn21  +retn22  +retn23  +retn24 
 +retn25  +retn26  +retn27  +retn28  +retn29   +retn30  +retn31  +retn32  +retn33  +retn34  +retn35  +retn36)/36;


 fvret12 =   ((retn1-mret12)**2+(retn2-mret12)**2+(retn3-mret12)**2+(retn4-mret12)**2+(retn5 -mret12)**2+(retn6-mret12)**2+(retn7-mret12)**2+(retn8-mret12)**2+(retn9-mret12)**2+(retn10-mret12)**2+(retn11-mret12)**2+(retn12-mret12)**2)/11 ;
fvret24=   ((retn1-mret24)**2+(retn2-mret24)**2+(retn3-mret24)**2+(retn4-mret24)**2+(retn5-mret24)**2+(retn6-mret24)**2+(retn7-mret24)**2+(retn8-mret24)**2+(retn9-mret24)**2+(retn10-mret24)**2+(retn11-mret24)**2+(retn12 
 -mret24)**2+(retn13-mret24)**2+(retn14-mret24)**2+(retn15-mret24)**2+(retn16-mret24)**2+(retn17-mret24)**2+(retn18-mret24)**2+(retn19-mret24)**2+(retn20-mret12)**2+(retn21-mret12)**2+(retn22-mret12)**2+(retn23-mret12)**2+(retn24-mret24)**2)/23;
fvret36 = ((retn1-mret36)**2+(retn2-mret36)**2+(retn3-mret36)**2+(retn4-mret36)**2+(retn5-mret36)**2+(retn6-mret36)**2+(retn7-mret36)**2+(retn8-mret36)**2+(retn9-mret36)**2+(retn10-mret36)**2+(retn11-mret36)**2+(retn12 
 -mret36)**2+(retn13-mret36)**2+(retn14-mret36)**2+(retn15-mret36)**2+(retn16-mret36)**2+(retn17-mret36)**2+(retn18-mret36)**2+(retn19-mret36)**2+(retn20-mret36)**2+(retn21-mret36)**2+(retn22-mret36)**2+(retn23-mret36)**2+(retn24 
 -mret36)**2+(retn25-mret36)**2+(retn26-mret36)**2+(retn27-mret36)**2+(retn28-mret36)**2+(retn29-mret36)**2+(retn30-mret36)**2+(retn31-mret36)**2+(retn32-mret36)**2+(retn33-mret36)**2+(retn34-mret36)**2+(retn35-mret36)**2+(retn36-mret36)**2)/35;


fsret12 = fvret12**.5;
fsret24 = fvret24**.5;
fsret36 = fvret36**.5;
proc sort;
by permno;


proc download data=ret out="C:\wald\fmonthreturns9209"; 
run;






/* End server statement block and close WRDS remote session. */
endrsubmit;
signoff;
